var app = angular.module("myApp",[]);
 app.controller("myController",function ($scope,$http){
 	$scope.zhen = false;
 	$scope.pageSize = 5;
 	$scope.page = 0;
 	$scope.jiu = {},
		
	$scope.addBtn = function () {
		$scope.title = "新增用户";
		$scope.zhen = false;
		$("#newDlg").modal();
	}

	$scope.saveBtn = function () {
		$scope.hobbies = "爬山";
		$scope.img = "12.png";
		$http({
			url:"server/ajaxReg.php",
			method:"get",
			params:$scope.user
		}).success(function(response){
			$("#newDlg").modal("hide");
			$scope.currentIndex = 0;
			$scope.page = 0;
			$scope.user="";
			renderTable();
		})
	}

	$scope.delBtn = function () {
		if (confirm("确定要删除该用户吗？")) {
			$http({
				url:"server/ajaxDelUser.php",
				method:"get",
				params:{
					id:this.user.id
				}
			}).success(function (response) {
				$scope.page = $scope.jiu;
				renderTable();
			})
		}
	}

	$scope.updataBtn = function () {
		$("#newDlg").modal();
		$scope.zhen = true;
		$scope.title = "修改用户";
		$scope.user = this.user;
	}

	$scope.dbUpdataBtn = function() {
		
		this.user.hobbies = "爬山|游泳|篮球";
		$http({
			url:"server/ajaxUpdateUser.php",
			method:"get",
			params:this.user
		}).success(function(response) {
			console.log($scope.jiu)
			$scope.page = $scope.jiu;
			renderTable();
			$("#newDlg").modal("hide");
		})
	}

	$scope.searchBtn = function() {
		$scope.page = $scope.jiu;
		renderTable();
	}

	$('.form-control').keydown(function(e) {
        if (e.keyCode == 13)
           $('.search').trigger('click')
    })

    $scope.pageClick = function () {
    	$scope.page = this.index;
    	$scope.currentIndex = this.index;
    	$scope.jiu = this.index;
    	renderTable();
    }

    $scope.firstClick = function () {
    	$scope.page = 0;
    	$scope.currentIndex = 0;
    	renderTable();
    }

    $scope.lastClick = function () {
    	$scope.page = $scope.pageArr.length-1;
    	$scope.currentIndex = $scope.pageArr.length-1;
    	renderTable();
    }

    function renderTable() {
		$http({
		url:"server/ajaxUserList.php",
		method:"get",
		params:{query:$scope.query,size:$scope.pageSize,page:$scope.page}

	}).success(function (response){
		/*console.log($scope.query);
		console.log($scope.pageSize);
		console.log($scope.page);*/
		$scope.users = response.data;
		$scope.total = response.total;
		$scope.page = Math.ceil($scope.total / $scope.pageSize);
		$scope.pageArr = [];
		for (var i=0; i<$scope.page; i++) {
			$scope.pageArr.push(i);
		}
	})
};
 	renderTable();
});